package br.com.iswe.gwt.ui.client.comp;

import java.util.ArrayList;
import java.util.List;

import br.com.iswe.gwt.ui.client.ISWEImageFactory;
import br.com.iswe.gwt.util.client.DateValidator;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class DateBox extends Composite {

	private static final String IMG = "_img";
	private static final String ID = "id";
	private static final String GWT_TEXT_BOX_DATE = "gwt-TextBox-date";
	
	protected HorizontalPanel horizontalPanel;
	protected TextBox dateTextBox;
	protected Image   imgCalendar;
	
	public DateBox(){
		//Inicia os componentes
		this.imgCalendar = ISWEImageFactory.getCommonImageBundle().calendario().createImage();
		this.dateTextBox = new TextBox();
		this.horizontalPanel = new HorizontalPanel();
		this.horizontalPanel.add(dateTextBox);
		this.horizontalPanel.add(imgCalendar);
		initWidget(this.horizontalPanel);
		
		//estilo do date
		this.dateTextBox.addStyleName(GWT_TEXT_BOX_DATE);
		//Configura os validadores para data
		this.dateTextBox.addKeyboardListener(DateValidator.DATE_CONSTRAINTS);
		//configura o maxlength
		this.dateTextBox.setMaxLength(10);
	}

	public TextBox getDateTextBox() {
		return dateTextBox;
	}

	public void setDateTextBox(TextBox dateTextBox) {
		this.dateTextBox = dateTextBox;
	}
	
	protected void abrirCalendario(){
		//Ao clicar na imagem abre o calendario
		CalendarBox.getInstance().showFor(this);
	}

	protected void onLoad() {
		//apos ter sido adicionado ao document configura o calendar
		//ClientUtils.setupCalendar(this.dateTextBox, this.imgCalendar);
		
		imgCalendar.addClickListener(new ClickListener(){public void onClick(Widget sender) {
			abrirCalendario();
		}});
		
	}
	
	protected String name = null;
	public void setName(String nome) {
		this.name = nome;
		this.dateTextBox.setName(nome);
		//configura o id dos objetos.
		DOM.setElementProperty(this.dateTextBox.getElement(), ID, nome.replace('.', '_'));
		DOM.setElementProperty(this.imgCalendar.getElement(), ID, nome.replace('.', '_') + IMG);
	}
	public String getName() {
		return name;
	}

	public void setEnabled(boolean b) {
		if(this.dateTextBox != null){
			this.dateTextBox.setEnabled(b);
			this.imgCalendar.setVisible(b);
		}
	}
	
	public void clear() {
		getDateTextBox().setText("");		
	}

	public void fireOnChangeEvent(Widget sender){
		//varre a lista de change listener
		for (int i = 0; i < changeListeners.size(); i++) {
			((ChangeListener)changeListeners.get(i)).onChange(sender);
		}
	}
	
	private List changeListeners = new ArrayList();
	public void addChangeListener(ChangeListener listener) {
		changeListeners.add(listener);
		getDateTextBox().addChangeListener(new ChangeListener(){public void onChange(Widget sender) {
			fireOnChangeEvent(sender);
		}});
		getDateTextBox().addFocusListener(new FocusListener(){public void onFocus(Widget sender) {}
		public void onLostFocus(Widget sender) {
			fireOnChangeEvent(sender);
		}});
	}

	
}
